package org.tobuy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tobuy.factory.UserDAOFactory;
import org.tobuy.vo.User;

public class RegistrationServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = "Registration_pass.jsp";
		String userid = req.getParameter("userid");
		String userpass = req.getParameter("userpass");
		String username = req.getParameter("username");
		String address = req.getParameter("address");
		List<String> info = new ArrayList<String>();  // collect information
		if(userid==null || "".equals(userid)){
			info.add("id can not be empty!!");
		}
		if(userpass==null || "".equals(userpass)){
			info.add("password can not be empty!!");
		}
		if(username==null || "".equals(username)){
			info.add("name can not be empty!!");
		}
		if(address==null || "".equals(address)){
			info.add("address can not be empty!!");
		}
		if(info.size()==0){  // no mistake
			User user = new User();
			user.setUser_id(userid);
			user.setUser_name(username);
			user.setUser_pass(userpass);
			user.setAddress(address);
			try{
				if(UserDAOFactory.getIUserDAOInstance().addUser(user)){
					//info.add("Registration successfully completed!");
				}else{
					info.add("Registartion failed! Pleas try again!");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		req.setAttribute("info", info);
		req.getRequestDispatcher(path).forward(req, resp);
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		this.doGet(req, resp);
	}

}
